<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateOperationLogTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        if (!Schema::hasTable('operation_logs')) { //如果不存在则建表
            Schema::create('operation_logs', function (Blueprint $table) {
                $this->create_table($table);
            });
        } else { //否则增量添加字段
            Schema::table('operation_logs', function (Blueprint $table) {
                $this->create_table($table);
            });
        }
        DB::statement("alter table `operation_logs` comment'操作日志表'");
    }
    public function create_table($table)
    {
        $table->engine = 'InnoDB';
       
        if (!Schema::hasColumn('operation_logs', 'id')) {
            $table->increments('id');
        }
        if (!Schema::hasColumn('operation_logs', 'uid')) {
            $table->integer('uid')->default(0)->comment('管理员id');
        }

        if (!Schema::hasColumn('operation_logs', 'path')) {
            $table->string('path', 32)->default('')->comment('路径');
        }

        if (!Schema::hasColumn('operation_logs', 'method')) {
            $table->string('method', 50)->default('')->comment('模块');
        }
        if (!Schema::hasColumn('operation_logs', 'ip')) {
            $table->string('ip', 80)->default('')->comment('ip地址');
        }
        if (!Schema::hasColumn('operation_logs', 'sql')) {
            $table->text('sql')->comment('sql语句');
        }
        if (!Schema::hasColumn('operation_logs', 'input')) {
            $table->text('input')->comment('操作信息');
        }
        if (!Schema::hasColumn('operation_logs', 'created_at')) {
            $table->timestamps();
        }
    }
    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        // Schema::dropIfExists('operation_log');
    }
}
